<template>
    <has-left-nav-layout>
        <template v-slot:leftNav>
            <left-menu :lists="menus" v-model="curMenu" @change="getData"/>
        </template>
        <main-wrap ref="mianEle" :loading="true" :load="onLoad" :end="page.finished" :noFixed="true" bodyBg="none" :showOverTxt="page.lists.length>0">
            <template v-slot:header>
                <div class="adheader">
                    <div class="hcon">
                        <div class="shopLetfTop">
                            <b>{{total}}</b> {{titleText}}
                        </div>
                        <div class="topSearch">
                            <div class="tswd">
                                 <my-search :placeholder="search" v-model="keyword"  @onSearch="getSearchData"/>
                            </div>
                        </div>
                    </div>
                </div>
            </template>
            <div class="goodsMainCon">
                <!-- <div class="goodsTabSearchBox">
                    <sort :lists="sortData" @change="getSortData"/>
                </div> -->
                <goods-list :lists="page.lists" @onClick="openDetail" :type="4"/>
                <emptyGame v-if="page.finished && page.lists.length<1" title="No Game"/>
            </div>
        </main-wrap>
        <my-dailog v-model:show="showFlag" :showtopclose="true" :styleType="5">
            <detail :id="itemDetail.arPropId"/>
        </my-dailog>
    </has-left-nav-layout>
</template>

<script>
import { reactive,toRefs,getCurrentInstance,onMounted,computed} from 'vue'
import {useRoute,useRouter} from 'vue-router'
import { useStore } from 'vuex'
import hasLeftNavLayout from '@/components/hasLeftNavLayout.vue'
import leftMenu from '@/components/leftMenu.vue'
import mainWrap from '@/components/mainWrap.vue'
import mySearch from '@/components/mySearch.vue'
import Image from '@/components/image.vue';
import goodsList from '@/components/goodsList.vue';
import sort from '@/components/sort.vue';
//import twoTab from '@/components/twoTab.vue'
import detail from './components/detail.vue'
import emptyGame from '@/components/emptyGame.vue'
import { useI18n } from 'vue-i18n'

export default {
    components:{
       hasLeftNavLayout,
       leftMenu,
       mainWrap,
       mySearch,
       Image,
       goodsList,
       sort,
       //twoTab,
       detail,
       emptyGame
    },
    props:{

    },
    setup(props,context){
        const router = useRouter()
        const route = useRoute()
        const store = useStore()
        const {proxy} = getCurrentInstance()
        const {t} = useI18n()

        const state = reactive({
            userInfo:computed(()=>store.state.base.userInfo),
            keyword:'',
            page:{
                lists:[],
                pageIndex:1,
                pageSize:20,
                finished: false
            },
            menus:[
                {name:'My',num:0,id:'prop_tab_1'},
                {name:'Whole',num:0,id:'prop_tab_2'}
            ],
            curMenu:'prop_tab_1',
            sortData:[
                {id:1,name:'default',noShow:true},
                {id:2,name:'Value'},
                {id:3,name:'Visits'},
                //{id:4,name:'Advertising'}
            ],
            sortParams:{
                orderField: 1,
                orderType: "desc",
            },
            showFlag:false,
            total:0,
            itemDetail:'',
            arLandmarkId:'',
            titleText:t('public.total'),
            search:t('public.search')
        })
        onMounted(()=>{
            state.arLandmarkId = route.query.cityid
            // 获取城市下的数据时，重新设置菜单
            if(state.arLandmarkId){
                state.menus=[
                    // {name:'Ad',key:'adNum', num:0,path:`/ad/space?cityid=${state.arLandmarkId}`},
                    // {name:'Prop',key:'propNum',num:0,path:`/prop?cityid=${state.arLandmarkId}`},
                    // {name:'Shop',key:'shopNum',num:0,path:`/shop/list?cityid=${state.arLandmarkId}`},
                    // {name:'Game',key:'gameNum',num:0,path:`/game?cityid=${state.arLandmarkId}`},
                    // {name:'Venues',key:'VenuesNum',num:0,path:`/venues?cityid=${state.arLandmarkId}`}
                    {name:t('prop.tab8'),key:'adNum', num:0,path:`/ad/space?cityid=${state.arLandmarkId}`},
                    {name:t('prop.tab9'),key:'propNum',num:0,path:`/prop?cityid=${state.arLandmarkId}`},
                    {name:t('prop.tab10'),key:'shopNum',num:0,path:`/shop/list?cityid=${state.arLandmarkId}`},
                    {name:t('prop.tab11'),key:'gameNum',num:0,path:`/game?cityid=${state.arLandmarkId}`},
                    {name:t('prop.tab12'),key:'VenuesNum',num:0,path:`/venues?cityid=${state.arLandmarkId}`}
                ]
                getCityTotal()  // 获取数量
            }
            if(route.query.id){
                // 直接打开对应id的详情弹窗
                state.itemDetail={}
                state.itemDetail.arPropId = route.query.id
                state.showFlag = true
            }
            onLoad()
            
            // if(!state.arLandmarkId){
            //     getTotal()
            // }
        })
        // 获取数量
        // const getTotal = async ()=>{
        //     store.dispatch("ad/get_prop_list", {
        //         page:1,
        //         limit:10
        //     }).then(res =>{
        //         state.menus[1].num = res.total || '0' 
        //     })
        //     store.dispatch("ad/get_prop_list", {
        //         page:1,
        //         limit:10,
        //         userAddress:state.userInfo.walletAddress
        //     }).then(res =>{
        //         state.menus[0].num = res.total || '0' 
        //     })
        // }
        const onLoad = ()=> {
            let userAddress = state.curMenu == 'prop_tab_1'?state.userInfo.walletAddress:''
            if(state.arLandmarkId){ // 是获取城市下的数据
                userAddress = ''
            }
            // 此函数必须返回一个Promise对象
            return new Promise((resolve, reject) => {
                setTimeout(async () => {
                    const res = await store.dispatch("ad/get_prop_list", {
                        page: state.page.pageIndex,
                        limit: state.page.pageSize,
                        ...state.sortParams,
                        name:state.keyword,
                        arLandmarkId:state.arLandmarkId,
                        userAddress
                    });
                    state.page.lists =[] //[...state.page.lists, ...res.data];
                    state.page.pageIndex++;
                    state.total = 0//res.total
                    //if (state.page.lists.length >= res.total) {
                        state.page.finished = true;
                    //}
                    resolve(true);
                }, 500);
            });
        }

        // 获取城市下的广告位、道具、店铺等数量
        const getCityTotal = ()=>{
            store.dispatch('base/get_arLandmarkek_detail',{
                arLandmarkId:state.arLandmarkId,
                walletAddress:state.userInfo.walletAddress
            }).then(res =>{
                if(res.success){
                   state.menus.map((item)=>{
                         if(res.data[item.key]){
                              item.num=res.data[item.key]||'0'
                         }else{
                             item.num='0'
                         }
                         return item
                     })
                }
            })
        }


        const resetScroll = async()=>{
            proxy.$refs.mianEle.stopScroll()
            state.page.lists=[]
            state.page.pageIndex=1
            state.page.finished=false
            await onLoad()
            proxy.$refs.mianEle.initScroll()
        }
        const getSearchData = async(keyword)=>{
            resetScroll()
        } 
        const getSortData = sortParams =>{
            state.sortParams = sortParams
            resetScroll()
        }

        const getData = id=>{
            console.log("YYYY:",id)
            resetScroll()
        }
        const openDetail = item=>{
            state.itemDetail = item
            state.showFlag = true
        }

        return{
            ...toRefs(state),
            onLoad,
            getData,
            openDetail,
            getSortData,
            getSearchData
        }
    }
}
</script>

<style lang="scss" scoped>
    .adheader{
        padding: 15px 30px 15px 0;
        font-size: 22px;
        font-weight: 500;
        color: #FFFFFF;
        font-family: Inter-Medium, Inter;
        font-weight: 500;
        .hcon{
            display: flex;
            height: 80px;
            line-height: 80px;
            align-items: center;
            .shopLetfTop{
                padding: 0;
                font-size: 28px;
                font-weight: 500;
                font-family: Inter-Medium, Inter;
                b{
                    color: #82FEF4;
                }
            }
            .topSearch{
                flex: 1;
                // display: flex;
                // justify-content: end;
                .tswd{
                    float:right;
                    width: 550px;
                }
            }
        }
    }
    .goodsMainCon{
        padding: 10px 0 0 0;
    }
    .goodsTabSearchBox{
        display: inline-block;
        height: 55px;
        background: url(/images/gloabAdSplace/tsc.png) left top no-repeat;
        background-size: 100% 100%;
        padding: 0 30px;
    }
    .ctgbox{
        width: 100%;
        padding: 0 0 20px 0;
    }
    .ads-detail-box{
        width: 100%;
        height: 100%;
        padding: 30px 30px 30px 105px;
        .ads-main-bg{
            position: relative;
            z-index: 100;
            height: 100%;
            width: 100%;
            padding: 30px;
            background: url(/images/bigdg2.png) left top no-repeat;
            background-size: 100% 100%;
            overflow: hidden;
            .ads-close{
                position: absolute;
                z-index: 100;
                right: 35px;
                top: 35px;
                font-size: 40px;
                color: #67F8FF;
            }
        }
    }
</style>